package hlhj.fhp.supreme

import android.app.Application
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMOptions
import com.lzy.okgo.OkGo
import com.lzy.okgo.cookie.CookieJarImpl
import com.lzy.okgo.cookie.store.MemoryCookieStore
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import com.mob.MobApplication
import com.mob.MobSDK
import hlhj.fhp.supreme.utils.MyUtils
import okhttp3.OkHttpClient
import java.util.logging.Level
import fhp.hlhj.giantfold.utils.LogUtil
import android.app.ActivityManager
import android.content.Context
import android.content.pm.PackageManager
import com.hyphenate.easeui.EaseUI
import com.hyphenate.easeui.domain.EaseEmojicon
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas.getData
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas


/**
 * Created by Administrator on 2018\3\6 0006.
 */
class MyApp :Application() {
    override fun onCreate() {
        super.onCreate()
        /*环信*/
        val options =  EMOptions();
// 默认添加好友时，是不需要验证的，改成需要验证
        options.acceptInvitationAlways = false;
// 默认添加好友时，是不需要验证的，改成需要验证
        val appContext = this
        val pid = android.os.Process.myPid()
        val processAppName = getAppName(pid)
// 如果APP启用了远程的service，此application:onCreate会被调用2次
// 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
// 默认的APP会在以包名为默认的process name下运行，如果查到的process name不是APP的process name就立即返回
        if (processAppName == null || !processAppName!!.equals(appContext.getPackageName(), ignoreCase = true)) {

            // 则此application::onCreate 是被service 调用的，直接返回
            return
        }
//初始化
        EaseUI.getInstance().init(applicationContext, options);
        EMClient.getInstance().init(applicationContext, options);
//在做打包混淆时，关闭debug模式，避免消耗不必要的资源
        EMClient.getInstance().setDebugMode(true);

        /*Okgo*/
        OkGo.getInstance().init(this)
        val builder= OkHttpClient.Builder()
        builder.cookieJar(CookieJarImpl(MemoryCookieStore()))
        val loggingInterceptor = HttpLoggingInterceptor("OkGo")
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
        loggingInterceptor.setColorLevel(Level.INFO)
        builder.addInterceptor(loggingInterceptor)
        OkGo.getInstance().okHttpClient = builder.build()
        /*MyUtil*/
        MyUtils.initUtil(applicationContext)
        /*MOb*/
        MobSDK.init(this)
        /*WebView*/
    }

    private fun getAppName(pID: Int): String? {
        var processName: String? = null
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val l = am.runningAppProcesses
        val i = l.iterator()
        val pm = this.packageManager
        while (i.hasNext()) {
            val info = i.next() as ActivityManager.RunningAppProcessInfo
            try {
                if (info.pid == pID) {
                    processName = info.processName
                    return processName
                }
            } catch (e: Exception) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }

        }
        return processName
    }
}